using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.SceneManagement;

public enum Dir
{
    Up,
    Forward
}

public class Player : MonoBehaviour
{
    private float speed = 300f;

    private Rigidbody2D rd;

    public PlayerAnim playerAnim;

    private bool is_OnGround = true;

    private int jumpNum = 1;

    public Transform[] points;  //0: front; 1: up; 2: throw

    public Transform curPoint;

    public int maxHealth = 3;
    public int curHealth;
    public int maxgrenade = 10;
    public int curGrenade;

    public bool is_Dead = false;
    public bool is_Resume = false;

    private GameUI gameUI;

    public bool is_Fired = false;
    public bool is_throw = false;

    private PlayerAttack playerAttack;

    // Start is called before the first frame update
    void Start()
    {
        rd = GetComponent<Rigidbody2D>();
        playerAnim = GetComponent<PlayerAnim>();
        playerAttack = GetComponent<PlayerAttack>();
        curHealth = maxHealth;
        curGrenade = maxgrenade;
        gameUI = GameObject.Find("Canvas").GetComponent<GameUI>();
    }

    // Update is called once per frame
    void Update()
    {
        if(is_Dead || is_Resume)
        {
            return;
        }

        if (Input.GetKey(KeyCode.W) && Input.GetKeyDown(KeyCode.J))
        {
            Fire(Dir.Up);
            return;
        }
        else if (Input.GetKeyDown(KeyCode.J))
        {
            if (playerAttack.HurtEnemys())
            {
                playerAttack.HurtEnemys();
                playerAnim.PlayAttackAnim();
                Soundmgr.Instance.PlayMusicByName("tieguo", Camera.main.transform.position, 0.7f);
            }
            else
            {
                Fire(Dir.Forward);
            }
            return;
        }

        if (Input.GetKeyDown(KeyCode.L) && curGrenade > 0)
        {
            curGrenade--;
            Throw();
            gameUI.UpdateGrenade(curGrenade);
            return;
        }
        PlayerMove();
        PlayerJump();
    }

    public void PlayerMove()
    {

        if (!is_OnGround)
        {
            return;
        }

        float h = Input.GetAxis("Horizontal");
        if (h > 0) 
        {
            transform.rotation = Quaternion.Euler(0, 0, 0);
            
            rd.velocity = new Vector2(h * speed * Time.fixedDeltaTime, rd.velocity.y);
            
            playerAnim.PlayWalkAnim();
        }else if(h < 0)
        {
            transform.rotation = Quaternion.Euler(0, 180, 0);

            rd.velocity = new Vector2(h * speed * Time.fixedDeltaTime, rd.velocity.y);

            playerAnim.PlayWalkAnim();
        }
        else
        {
            playerAnim.PlayIdleAnim();
        }
    }

    public void PlayerJump()
    {
        if (Input.GetKeyDown(KeyCode.K) && jumpNum > 0)
        {
            is_OnGround = false;
            jumpNum -= 1;
            rd.AddForce(Vector2.up * 600f);
            playerAnim.PlayJumpAnim();
        }

    }

    public void OnCollisionEnter2D(Collision2D other)
    {
        if(other.transform.tag == "Ground" || other.transform.tag == "Enemy")
        {
            is_OnGround = true;
            jumpNum = 1;
        }
    }

    public void Fire(Dir dir)
    {
        if (is_Fired) { return; }
        Soundmgr.Instance.PlayMusicByName("shoot",Camera.main.transform.position,1f);
        GameObject temp = Resources.Load<GameObject>("Prefabs/Bullet");

        switch (dir)
        {
            case Dir.Forward:
                {
                    curPoint = points[0];
                    playerAnim.PlayShootAnim();
                    break;
                }
            case Dir.Up:
                {
                    curPoint = points[1];
                    playerAnim.PlayShootupAnim();
                    break;
                }
        }

        GameObject go = Instantiate(temp, curPoint.transform.position, Quaternion.identity);
        go.GetComponent<Bullet>().InitDir(dir);
        is_Fired = true;
        StartCoroutine(firinginterval());
    }
    public IEnumerator firinginterval()
    {
        yield return new WaitForSecondsRealtime(0.3f);
        is_Fired = false;
    }

    public void Throw()
    {
        if (is_throw) { return; }
        playerAnim.PlayThrowAnim();
        GameObject temp = Resources.Load<GameObject>("Prefabs/Grenade");
        GameObject go = Instantiate(temp, points[2].transform.position, Quaternion.identity);
        is_throw = true;
        StartCoroutine(throwinterrval());
    }

    public IEnumerator throwinterrval()
    {
        yield return new WaitForSecondsRealtime(0.6f); 
        is_throw = false;
    }

    public void Hurt()
    {
        curHealth -= 1;
        if (gameUI != null)
        {
            gameUI.UpdateHealth(curHealth);
        }
        if (curHealth <= 0)
        {
            is_Dead = true;
            playerAnim.PlayDieAnim();
            Soundmgr.Instance.PlayMusicByName("soliderDie", Camera.main.transform.position, 1f);
            GameObject.Destroy(GameObject.Find("down"));
            StartCoroutine(LoadScene0());
        }
        else
        {
            is_Resume = true;

            StartCoroutine(Resume());
        }
    }

    public IEnumerator LoadScene0()
    {
        yield return new WaitForSecondsRealtime(3f);
        SceneManager.LoadScene(0);
        GameObject.Destroy(gameObject);
    }

    public IEnumerator Resume()
    {
        yield return new WaitForSeconds(1);
        playerAnim.PlayResumeAnim();

        transform.position = new Vector3(transform.position.x - 3f, transform.position.y + 5f);
        playerAnim.PlayIdleAnim();

        StartCoroutine(ResetState());

    }

    public IEnumerator ResetState()
    {
        yield return new WaitForSeconds(1f);
        is_Resume = false;
    }

    public bool GetCurState()
    {
        if (is_Dead)
        {
            return false;
        }
        else if(is_Resume)
        {
            return false;
        }
        return true;
    }
}
